package com.changgou.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.pay.service.WeixinPayService;
import com.changgou.util.HttpClient;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/*****
 * @Author: 黑马训练营
 * @Description: com.changgou.pay.service.impl
 ****/
@Service
public class WeixinPayServiceImpl implements WeixinPayService {

    @Value("${weixin.appid}")
    private String appid;

    @Value("${weixin.partner}")
    private String partner;

    @Value("${weixin.partnerkey}")
    private String partnerkey;

    @Value("${weixin.notifyurl}")
    private String notifyurl;

    /***
     * 查询订单状态
     * @param outtradeno : 客户端自定义订单编号
     * @return
     */
    @Override
    public Map queryPayStatus(String outtradeno) throws Exception{
        //封装请求参数
        Map<String,String> dataMap = new HashMap<String,String>();
        dataMap.put("appid",appid); //应用ID
        dataMap.put("mch_id",partner);  //商户号
        dataMap.put("out_trade_no",outtradeno); //订单编号
        dataMap.put("nonce_str", WXPayUtil.generateNonceStr()); //随机字符串

        //将Map转成XML字符串，并且生成签名,将请求参数转成XML字符串
        String xmlParameters = WXPayUtil.generateSignedXml(dataMap, partnerkey);

        //请求
        HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");

        //参数
        httpClient.setXmlParam(xmlParameters);

        //属于Https协议
        httpClient.setHttps(true);

        //使用Post提交
        httpClient.post();

        //获取返回结果 xml格式
        String result = httpClient.getContent();

        //将返回结果转成Map
        return  WXPayUtil.xmlToMap(result);
    }

    /***
     * 创建预支付记录，获取支付二维码地址
     * outtradeno:订单号
     * totalfee:支付金额，单位分
     * @return
     */
    @Override
    //public Map createNative(String outtradeno, String totalfee) throws Exception {
    public Map createNative(Map<String,String> parameterMap) throws Exception {
        //封装请求参数
        Map<String,String> dataMap = new HashMap<String,String>();
        dataMap.put("appid",appid); //应用ID
        dataMap.put("mch_id",partner);  //商户号
        dataMap.put("nonce_str", WXPayUtil.generateNonceStr()); //随机字符串
        dataMap.put("body","畅购商品");
        //============================
        dataMap.put("out_trade_no",parameterMap.get("outtradeno")); //订单编号
        dataMap.put("total_fee",parameterMap.get("totalfee"));      //交易金额，单位：分
        //============================
        //队列参数名字 queue
        Map<String,String> attachMap = new HashMap<String,String>();
        attachMap.put("queue",parameterMap.get("queue"));
        attachMap.put("exchange",parameterMap.get("exchange"));
        //用户名 username，适用于秒杀订单
        String username = parameterMap.get("username");
        if(!StringUtils.isEmpty(username)){
            attachMap.put("username",username);
        }

        String attachjson = JSON.toJSONString(attachMap);
        dataMap.put("attach",attachjson);

        dataMap.put("spbill_create_ip","127.0.0.1");
        //该地址用于接收微信服务器发送的微信支付状态
        dataMap.put("notify_url",notifyurl);
        dataMap.put("trade_type","NATIVE");
        //将Map转成XML字符串，并且生成签名,将请求参数转成XML字符串
        String xmlParameters = WXPayUtil.generateSignedXml(dataMap, partnerkey);

        //请求
        HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");

        //参数
        httpClient.setXmlParam(xmlParameters);

        //属于Https协议
        httpClient.setHttps(true);

        //使用Post提交
        httpClient.post();

        //获取返回结果 xml格式
        String result = httpClient.getContent();

        //将返回结果转成Map
        return  WXPayUtil.xmlToMap(result);
    }
}
